package com.cskaoyan.service;

import com.cskaoyan.bean.Avator;
import com.cskaoyan.bean.data.WxLoginData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.domain.MarketUserExample;
import com.cskaoyan.mapper.MarketUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    Avator avator;

    @Override
    public MarketUser queryUserAndName(String username) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        return marketUserMapper.selectOneByExample(marketUserExample);
    }

    @Override
    public BaseRespVo registerUser(String code, String mobile, String password, String username,String realCode) {
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andUsernameEqualTo(username);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if(marketUsers.size() !=0){
            return BaseRespVo.fail(704,"用户名已注册");
        }
        if(!mobile.matches("^1[3-9]\\d{9}")){
            return BaseRespVo.fail(402,"参数值不对");
        }
        MarketUserExample marketUserExample1 = new MarketUserExample();
        marketUserExample1.createCriteria().andMobileEqualTo(mobile);
        if(marketUserMapper.selectByExample(marketUserExample1).size()!=0){
            return BaseRespVo.fail(705,"手机号已注册");
        }
        if(!code.equals(realCode)){
            return BaseRespVo.fail(703,"验证码错误");
        }
        MarketUser record = new MarketUser();
        record.setUsername(username);
        record.setPassword(password);
        record.setMobile(mobile);
        record.setAddTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        record.setNickname(username);
        record.setAvatar(avator.getAvator());
        int i = marketUserMapper.insertSelective(record);
        WxLoginData.UserInfoBean userInfoBean = new WxLoginData.UserInfoBean();
        userInfoBean.setNickName(username);
        userInfoBean.setAvatarUrl(avator.getAvator());
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        subject.login(usernamePasswordToken);
        WxLoginData wxLoginData = new WxLoginData(userInfoBean,subject.getSession().getId().toString());
        return BaseRespVo.ok(wxLoginData);
    }

    @Override
    public BaseRespVo resetUser(String code, String mobile, String password, String realCode) {
        if(!mobile.matches("^1[3-9]\\d{9}")){
            return BaseRespVo.fail(402,"参数值不对");
        }
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andMobileEqualTo(mobile);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if(marketUsers.size()==0){
            return BaseRespVo.fail(403,"手机号未注册");
        }
        if(!code.equals(realCode)){
            return BaseRespVo.fail(703,"验证码错误");
        }
        if(password.equals(marketUsers.get(0).getPassword())){
            return BaseRespVo.fail(405,"当前密码与原来密码一样");
        }
        MarketUserExample marketUserExample1 = new MarketUserExample();
        marketUserExample1.createCriteria().andMobileEqualTo(mobile);
        MarketUser record = new MarketUser();
        record.setPassword(password);
        record.setUpdateTime(LocalDateTime.now());
        marketUserMapper.updateByExampleSelective(record,marketUserExample1);
        return BaseRespVo.ok();
    }
}
